package com.dyz.leetcode._12_linkedList;

import com.dyz.leetcode.ListNode;

import java.util.HashSet;
import java.util.Set;

public class _160_getIntersectionNode {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        Set<ListNode> setA = new HashSet<>();
        while (headA!=null){
            setA.add(headA);
            headA = headA.next;
        }
        while (headB!=null){
            if(setA.contains(headB)){
                return headB;
            }
            headB = headB.next;
        }
        return null;

    }

    public ListNode getIntersectionNode1(ListNode headA, ListNode headB) {
       if(headA==null|| headB==null){return null;}
       ListNode a = headA;
       ListNode b = headB;
       while (a!=b){
           a = (a==null)?headB:a.next;
           b = (b==null)?headA:b.next;
       }
       return a;

    }
}
